package com.buddy.qreport.web.report.controller;

import com.buddy.qreport.common.mybatis.entity.Page;
import com.buddy.qreport.query.entity.QueryEntity;
import com.buddy.qreport.query.entity.QueryType;
import com.buddy.qreport.query.params.QueryParam;
import com.buddy.qreport.query.service.IQueryService;
import com.buddy.qreport.report.entity.ReportAlbumEntity;
import com.buddy.qreport.report.entity.ReportEntity;
import com.buddy.qreport.report.entity.ReportRelationEntity;
import com.buddy.qreport.report.service.IReportService;
import com.buddy.qreport.template.IReportTemplate;
import com.buddy.qreport.template.config.TemplateManager;
import com.buddy.qreport.web.report.entity.ReportExecuteParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController("QREPORT_ReportController")
@RequestMapping("/report")
@Slf4j
public class ReportController {

    @Autowired
    @Qualifier("QREPORT_ReportService")
    private IReportService reportService;

    @Autowired
    @Qualifier("QREPORT_QueryService")
    private IQueryService queryService;

    @GetMapping("/deleteReport")
    public ResponseEntity deleteReport(@RequestParam("reportId") String reportId){

        reportService.deleteReport(reportId);

        return new ResponseEntity(HttpStatus.OK);
    }

    @PostMapping("/addReport")
    public ResponseEntity addReport(@RequestBody ReportEntity reportEntity){

        reportService.addReport(reportEntity);

        return new ResponseEntity(HttpStatus.OK);
    }

    @GetMapping("/getReportQueryParams")
    public ResponseEntity getReportQueryParams(@RequestParam("reportId") String reportId){

        List<QueryParam> result = Collections.EMPTY_LIST;

        ReportEntity reportEntity = reportService.getOne(reportId);

        if(reportEntity != null){

            String queryId = reportEntity.getReportQuery();

            if(queryId != null){

                QueryEntity queryEntity = queryService.getOne(queryId);


                result = queryEntity.getQuerParamsConfig();

                queryService.castQueryParams(queryEntity.getQueryDatasource(), result);

            }
        }

        return new ResponseEntity(result,HttpStatus.OK);

    }

    @PostMapping("/doQuery")
    public ResponseEntity doQuery(@RequestBody ReportExecuteParam param, HttpServletRequest request){

        List<Map> result = Collections.EMPTY_LIST;

        if(param.getReportId() != null){

            if(param.getParams() == null){
                param.setParams(Collections.EMPTY_MAP);
            }

            ReportEntity reportEntity = reportService.getOne(param.getReportId());

            String queryId = reportEntity.getReportQuery();

            if(queryId != null){

                QueryEntity queryEntity = queryService.getOne(queryId);

                if(queryEntity.getQueryType() == QueryType.SQL){

                    String sql = queryEntity.getQuerySql();

                    String datasourceId = queryEntity.getQueryDatasource();

                    result = queryService.dynamicQuerySQL(datasourceId,sql,param.getParams());

                }else{
                    String url = queryEntity.getQueryUrl();

                    String method = queryEntity.getQueryMethod();

                    Enumeration<String> headerNames = request.getHeaderNames();

                    Map<String,String> headers = new HashMap<>();

                    while (headerNames.hasMoreElements()){

                        String name = headerNames.nextElement();

                        headers.put(name,request.getHeader(name));
                    }

                    try {
                        result = queryService.dynamicQueryURL(url,method,param.getParams(),headers);
                    } catch (Exception e) {
                        //e.printStackTrace();
                        log.info("url请求失败:{}",e.getLocalizedMessage());

                        throw new RuntimeException(e);

                        //result = Collections.EMPTY_LIST;
                    }
                }

            }
        }

        return new ResponseEntity(result,HttpStatus.OK);
    }

    @GetMapping("/getTemplateByReport")
    public ResponseEntity getTemplateByReport(@RequestParam("reportId") String reportId){


        ReportEntity reportEntity = reportService.getOne(reportId);

        if(reportEntity != null){

            String templateName = reportEntity.getReportTemplate();

            IReportTemplate reportTemplate = TemplateManager.getInstance().getTemplateByName(templateName);

            if(reportTemplate != null){

                return new ResponseEntity(reportTemplate,HttpStatus.OK);
            }

        }

        return new ResponseEntity(HttpStatus.BAD_REQUEST);

    }

    @GetMapping("/getRelations")
    public ResponseEntity getRelations(@RequestParam("reportId") String reportId){

        List<ReportRelationEntity> relationEntities = reportService.getRelations(reportId);

        return new ResponseEntity(relationEntities, HttpStatus.OK);

    }


    @GetMapping("/getReportList")
    public ResponseEntity getReportList(Page page){

        if(page == null){
            page = new Page();
        }

        Page<ReportEntity> reportEntityPage = reportService.getReportlist(page);

        return new ResponseEntity(reportEntityPage,HttpStatus.OK);

    }

    @GetMapping("/getReportDetail")
    public ResponseEntity getReportDetail(@RequestParam("reportId") String reportId){
        return new ResponseEntity(reportService.getOne(reportId),HttpStatus.OK);
    }

    @GetMapping("/deleteReportAlbum")
    public ResponseEntity deleteReportAlbum(@RequestParam("albumId") String albumId){

        reportService.deleteAlbum(albumId);

        return new ResponseEntity(HttpStatus.OK);
    }

    @GetMapping("/getReportAlbumList")
    public ResponseEntity getReportAlbumList(Page page){

        if(page == null){
            page = new Page();
        }

        Page<ReportAlbumEntity> albumEntityPage = reportService.getReportAlbum(page);

        return new ResponseEntity(albumEntityPage,HttpStatus.OK);
    }

    @PostMapping("/saveAlbum")
    public ResponseEntity saveAlbum(@RequestBody ReportAlbumEntity entity){

        reportService.saveOrUpdateReportAlbum(entity);

        return new ResponseEntity(HttpStatus.OK);
    }

    @GetMapping("/getOneReportAlbum")
    public ResponseEntity getOneReportAlbum(@RequestParam("albumId") String albumId){

        ReportAlbumEntity entity = reportService.getOneReportAlbum(albumId);

        return new ResponseEntity(entity,HttpStatus.OK);
    }


}
